package com.luo.controller;

import com.alibaba.fastjson.JSON;
import com.luo.model.ResultObj;
import com.luo.service.CartService;
import com.luo.util.TokenId;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能：
 * 作者：Luo。
 * 日期：2024/2/14 13:21
 */
@WebServlet("/member/updateCart")
public class UpdateCartItemController extends HttpServlet {

    CartService cartService = new CartService();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String goodId = req.getParameter("id");
        String count = req.getParameter("count");
        String selected = req.getParameter("selected");
        Object userId = TokenId.getUserId(req, resp);
        if (userId != null && userId instanceof String){
            ResultObj resultObj = new ResultObj();
            Map map = new HashMap();
            map.put("userId",userId);
            map.put("selected",selected);
            map.put("count",count);
            map.put("goodId",goodId);
            int i = cartService.updateOneCartSelectedById(map);
            if (i > 0) {
                resultObj.setCode("1");
                resultObj.setMsg("添加成功");
            } else {
                resultObj.setCode("-1");
                resultObj.setMsg("添加失败");
            }
            String jsonString = JSON.toJSONString(resultObj);
            resp.getWriter().write(jsonString);
            resp.getWriter().close();
        }
    }
}
